package com.itheima.web.controller.system;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageInfo;
import com.itheima.domain.system.Role;
import com.itheima.utils.BeanUtil;
import com.itheima.web.controller.BaseServlet;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@WebServlet("/system/role")
public class RoleServlet extends BaseServlet {


    public void findByMethod(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //进入列表页
        //获取数据
        int page = 1;
        int size = 5;
        String value = null;
        if (StringUtils.isNotBlank(request.getParameter("page"))) {
            page = Integer.parseInt(request.getParameter("page"));
        }
        if (StringUtils.isNotBlank(request.getParameter("size"))) {
            size = Integer.parseInt(request.getParameter("size"));
        }
        if (StringUtils.isNotBlank(request.getParameter("str"))) {
            value = request.getParameter("str");
        }

        PageInfo all = roleService.findByMethod(page, size, value);
        //将数据保存到指定的位置
        request.setAttribute("page", all);
        request.setAttribute("str", value);
        //跳转页面
        request.getRequestDispatcher("/WEB-INF/pages/system/role/role-list.jsp").forward(request, response);
    }

    public void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String str = request.getParameter("str");
        if (StringUtils.isNotBlank(request.getParameter("str"))) {
            findByMethod(request, response);
            return;
        }
        //进入列表页
        //获取数据
        int page = 1;
        int size = 5;
        if (StringUtils.isNotBlank(request.getParameter("page"))) {
            page = Integer.parseInt(request.getParameter("page"));
        }
        if (StringUtils.isNotBlank(request.getParameter("size"))) {
            size = Integer.parseInt(request.getParameter("size"));
        }
        PageInfo all = roleService.findAll(page, size);
        //将数据保存到指定的位置
        request.setAttribute("page", all);
        //跳转页面
        request.getRequestDispatcher("/WEB-INF/pages/system/role/role-list.jsp").forward(request, response);
//        request.getRequestDispatcher("/pages/role-list.jsp").forward(request, response);
    }

    public void toAdd(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //跳转页面
        request.getRequestDispatcher("/WEB-INF/pages/system/role/role-add.jsp").forward(request, response);
    }

    public void save(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //将数据获取到，封装成一个对象
        Role role = BeanUtil.fillBean(request, Role.class);
        //调用业务层接口save
        roleService.save(role);
        //跳转回到页面list
        response.sendRedirect(request.getContextPath() + "/system/role?operation=list");
    }

    public void toEdit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //查询要修改的数据findById
        String id = request.getParameter("id");
        Role role = roleService.findById(id);
        //将数据加载到指定区域，供页面获取
        request.setAttribute("role", role);
        //跳转页面
        request.getRequestDispatcher("/WEB-INF/pages/system/role/role-update.jsp").forward(request, response);
//        request.getRequestDispatcher("/pages/role-update.jsp").forward(request, response);
    }

    public void edit(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //将数据获取到，封装成一个对象
        Role role = BeanUtil.fillBean(request, Role.class);
        System.out.println(role);
        //调用业务层接口save
        roleService.update(role);
        //跳转回到页面list
        response.sendRedirect(request.getContextPath() + "/system/role?operation=list");
    }

    public void deleteByIds(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String ids = request.getParameter("ids");
        String[] splitId = ids.split(",");
        for (String s : splitId) {
//            System.out.println(s);
            //调用业务层接口
            roleService.deleteByIds(s);
        }
        //跳转回到页面list
        response.sendRedirect(request.getContextPath() + "/system/role?operation=list");
    }

    public void author(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        //获取要授权的角色id
        String roleId = request.getParameter("id");
        //使用id查询对应的数据（角色id对应的模块信息）
        Role role = roleService.findById(roleId);
        request.setAttribute("role", role);
        //根据当前的角色id获取所有的权限数据，并加载关系数据
        List<Map> map = permissionService.findAuthorDataByRoleId(roleId);

//        System.out.println(map);

        //map转成json数据
        ObjectMapper om = new ObjectMapper();
        String json = om.writeValueAsString(map);
        request.setAttribute("rolePermissionJson", json);
        // TODO 数据未查询
        //跳转到树页面中
        request.getRequestDispatcher("/WEB-INF/pages/system/role/author.jsp").forward(request, response);
    }

    public void updateRolePermission(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        String roleId = request.getParameter("roleId");
        String permissionIds = request.getParameter("permissionIds");
        roleService.updateRolePermission(roleId, permissionIds);
        //跳转回到页面list
        response.sendRedirect(request.getContextPath() + "/system/role?operation=list");
    }
}